package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.admin.service.HtmlService;
import cn.hinglo.gameclaw.client.service.InvitationService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.data.utils.JwtUtil;
import cn.hinglo.gameclaw.user.bean.Html;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2018/1/3
 * @author: HingLo
 * @description: App端请求的一些HTML界面
 */
@RestController
@RequestMapping("/client/h/")
public class HtmlClientAction extends BaseController {


    /**
     * html 类型为邀请好友
     */
    private final static int INVITATION = 1;
    private final HtmlService htmlService;

    @Autowired
    public HtmlClientAction(HtmlService htmlService) {
        this.htmlService = htmlService;
    }

    @GetMapping("invitation")
    public Result invitationHtml() {
        String url = htmlService.invitationHtmlService(INVITATION);
        if (StrUtil.isBlank(url)) {
            return error("邀请功能已暂停");
        } else {
            String userId = getUserId(request);
            String token = JwtUtil.createJWT(userId, "");
            url = url + "?" + FieldUtils.AUTHORIZATION + "=" + token;
            Map<String, Object> map = new HashMap<>(3);
            map.put("url", url);
            map.put("friend_gold", InvitationService.FRIEND_GOLD);
            map.put("gold", InvitationService.GOLD);
            return success(map);
        }
    }

    /**
     * 通过HTML的唯一Id来返回html的访问链接
     *
     * @param id html 管理的
     * @return
     */
    @GetMapping("html")
    public Result html(@RequestParam("id") String id) {
        Html html = htmlService.htmlService(id);
        if (html != null) {
            String userId = getUserId(request);
            String token = JwtUtil.createJWT(userId, "");
            String url = FieldUtils.URL + html.getUrl() + "?" + FieldUtils.AUTHORIZATION + "=" + token;
            return success(url);
        } else {
            return error("该html不存在");
        }
    }

}
